import 'dart:io';

void main() async {
  var server = await HttpServer.bind(InternetAddress.anyIPv4, 8900);
  print('服务器启动，监听端口: ${server.address.host}:${server.port}');

  server.listen((HttpRequest request) {
    WebSocketTransformer.upgrade(request).then((WebSocket ws) {
      ws.listen(
            (message) {
          print('收到消息: $message');
          ws.add('服务器回复: $message');
        },
        onDone: () {
          print('客户端断开连接');
        },
        onError: (error) {
          print('发生错误: $error');
        },
      );
    });
  });
}
